package yh.plugins.workflow;

import java.sql.Connection;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beaut.project.util.FlowUtility;
import com.beaut.project.util.Utility;

import yh.core.data.YHRequestDbConn;
import yh.core.funcs.workflow.util.YHFlowRunUtility;
import yh.core.funcs.workflow.util.YHIWFPlugin;
import yh.core.global.YHBeanKeys;
import yh.core.util.PluginThread;

public class GeneratePlugin extends BasePlugin implements YHIWFPlugin{

	 /**
	   * 节点执行完毕执行
	   * @param request
	   * @param response
	   * @return
	   */
	  public String after(HttpServletRequest request, HttpServletResponse response) throws Exception {
	    //System.out.println("------------结束啦");
		  
		  try {
		      YHRequestDbConn requestDbConn = (YHRequestDbConn) request
		          .getAttribute(YHBeanKeys.REQUEST_DB_CONN_MGR);
		      Connection conn = requestDbConn.getSysDbConn();
		      //流程id
		      String flowIdStr = request.getParameter("flowId");
		      if(flowIdStr == null){
		    	  flowIdStr = String.valueOf(request.getAttribute("flowId"));
		      }
		      //工作id即流水号
		      String runIdStr = request.getParameter("runId");
		      if(runIdStr == null){
		    	  runIdStr = String.valueOf(request.getAttribute("runId"));
		      }
		      //实际步骤号,即现在执行到几步
//		      String prcsIdStr = request.getParameter("prcsId");
	      //设计步骤号,即现在执行所在的流程步骤
//		      String flowPrcsStr = request.getParameter("flowPrcs");
		      //用户选择的步骤号(不是实际步骤号),这时步骤号是以逗号分割的(并发时用户可能先多个步骤)，如：2,
		      String prcsChoose = request.getParameter("prcsChoose");
		      if(prcsChoose == null){
		    	  prcsChoose = String.valueOf(request.getAttribute("prcsChoose"));
		      }
//		      YHFlowRunUtility wf = new YHFlowRunUtility();
//		      int runId = Integer.parseInt(runIdStr);
//		      int prcsId = Integer.parseInt(prcsIdStr);
		      int flowId = Integer.parseInt(flowIdStr);
//		      int flowPrcs = Integer.parseInt(flowPrcsStr);
//		      YHFlowRunData rd =  wf.getFlowRunData(conn, flowId, runId, "项目部领导审批");	      
//		      String agreeStr = rd.getItemData();
		      String tableName=FlowUtility.getTableName(flowId);
		      if(tableName==null || tableName.equals("")){
		    	  return "代码中缺少相关配置，请联系开发商";
		      }
		      int updateStatus=this.updateBusinessStatus(tableName, FlowUtility.getTableFieldStatusName(flowId), prcsChoose, conn, runIdStr, flowId);
		      
		      
		      //只有通过了才要回调
		      if(updateStatus == agreed){
//		    	  if(updateStatus==this.agreed || updateStatus==this.noAgreed){
		    	String sn=request.getServerName();
		        int port=request.getServerPort();
		        String cp=request.getContextPath();
		        String url=FlowUtility.getBackUrl(flowId);
		        if(Utility.isNullorEmpty(url)){
		        	return null;
		        }
		        String businessKey=this.getBusinessId(conn, runIdStr, flowId);
		    	String urlBack="http://"+sn+":"+port+cp+url+"?key="+businessKey+"&status="+updateStatus;
		    	Cookie[] cookies = request.getCookies();
		    	String JSESSIONID = null;
		    	//获取用户名称与用户ID，用于流程持久化回调使用
		    	Map<String, Object> sessionMap = (Map<String, Object>) request.getSession().getAttribute("LOGIN_USER");
		    	String personId= sessionMap.get("id").toString();
		    	String person=sessionMap.get("truename").toString();
		    	
		    	for(Cookie c:cookies){
		    		if("JSESSIONID".equals(c.getName())){  
	                    JSESSIONID = c.getValue();  
	                    break;  
	                }  
		    	}
		    	PluginThread pthread=new PluginThread(urlBack,JSESSIONID,personId,person);
		    	pthread.start();
		      }
		     
		    } catch(Exception ex) {
		      throw ex;
		    }
		  
	    return null;
	  }
	@Override
	public String before(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


}
